package com.gxkj.projects.myshopx.controllers.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.dto.GoodsDto;
import com.gxkj.projects.myshopx.dto.ReturnData;
import com.gxkj.projects.myshopx.dtos.GalleryDto;
import com.gxkj.projects.myshopx.entitys.Attribute;
import com.gxkj.projects.myshopx.entitys.Goods;
import com.gxkj.projects.myshopx.entitys.GoodsAttr;
import com.gxkj.projects.myshopx.entitys.GoodsGallery;
import com.gxkj.projects.myshopx.enums.ErrorCodeEnum;
import com.gxkj.projects.myshopx.services.AttributeService;
import com.gxkj.projects.myshopx.services.GoodsService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import static com.gxkj.projects.myshopx.util.SessionUserUtil.getUser;

@Controller
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    private  static Logger LOG = LoggerFactory.getLogger(AdminGoodsController.class);

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private AttributeService attributeService;


    @RequestMapping(value="")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
        String mv = "admin/admin_goods";
        return mv;
    }

    @RequestMapping(value="dopage",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<ListPager<Goods>> dopage(Goods entity,
                                                  int pageno, int pagesize,
                                                  HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){

        ReturnData<ListPager<Goods>> returnData = new ReturnData<ListPager<Goods>>();
        ListPager<Goods> pager =  goodsService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(pager);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;

    }
    @RequestMapping(value="doadd",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Goods>doAdd(Goods entity,
                                   @RequestParam(value="goodsImgFile" ,required = false) CommonsMultipartFile goodsImgFile,
                                   @RequestParam(value="img_urls" ,required = false) CommonsMultipartFile[] img_urls,
                                   @RequestParam(value = "img_files",required = false)String[] img_files,
                                   MultipartHttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException, IOException {

        String[] img_descs =  request.getParameterValues("img_descs");

        String[] attr_id_list =  request.getParameterValues("attr_id_list");//属性ID
        String[] attr_value_list =  request.getParameterValues("attr_value_list");//属性值
        String[] attr_price_list =  request.getParameterValues("attr_price_list");//属性价格

        String[] real_goods_list =  request.getParameterValues("real_goods_list");//关联商品id

       // String  goodsDesc =  request.getParameterValues("goodsDesc");

        ReturnData<Goods> returnData = new ReturnData<Goods>();

        Map<String, MultipartFile> fileMap = request.getFileMap();
        List<GalleryDto> galleryDtoList = new ArrayList<GalleryDto>();
        if(img_urls != null){
            String pinIndex = null;
            for (int i = 0 ;i < img_urls.length;i++){

                GalleryDto dto  = new GalleryDto();
                dto.setImgDesc(img_descs[i]);
                dto.setImgUrl( "");
                dto.setUploadFile(img_urls[i]);

                galleryDtoList.add(dto);
            }
        }
        //商品属性
        List<GoodsAttr> goodsAttrList = new ArrayList<GoodsAttr>();
        if(attr_id_list!= null && attr_id_list.length > 0){
            int l = attr_id_list.length;
            for(int i=0;i<l;i++){
                GoodsAttr attr = new GoodsAttr();
                String  attr_id =  attr_id_list[i];
                String attr_value =   attr_value_list[i];
                attr.setAttrId(attr_id);
                attr.setAttrValue(attr_value);
                if(StringUtils.isNotBlank(attr_price_list[i])){
                    attr.setAttrPrice(new BigDecimal(attr_price_list[i]));
                }else {
                    attr.setAttrPrice(BigDecimal.ZERO);
                }
                goodsAttrList.add(attr);
            }
        }

        goodsService.doAddGoods(getUser(request),entity,goodsImgFile,galleryDtoList,goodsAttrList,null);

         returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="get",method= RequestMethod.GET)
    @ResponseBody
    public  ReturnData<GoodsDto>get(String id,
                                    HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException, InvocationTargetException, IllegalAccessException {

        ReturnData<GoodsDto> returnData = new ReturnData<GoodsDto>();
        GoodsDto dto = new GoodsDto();
        Goods entity = goodsService.getById(id);
        BeanUtils.copyProperties(dto,entity);

        List<GoodsAttr> dbGoodsAttrList = goodsService.selectGoodsAttrByGoodsId(id);
        List<GoodsGallery> dbGoodsGalleryList =  goodsService.selectGoodsGalleryByGoodsId(id);
        dto.setDbGoodsAttrList(dbGoodsAttrList);
        dto.setDbGoodsGalleryList(dbGoodsGalleryList);

        String goodtypeid = dto.getGoodsType();
        if(StringUtils.isNotBlank(goodtypeid)){
            List<Attribute> attributeList = attributeService.getAttributeListByTypeId(goodtypeid);
            dto.setStructureGoodsAttrList(attributeList);
        }

        returnData.setEntity(dto);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="doupdate",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Goods>doupdate(Goods entity ,
                                         HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Goods> returnData = new ReturnData<Goods>();
        //goodsService.doUpdateGoods(getUser(request),entity,null,null,null,null,null);
        returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }
    @RequestMapping(value="dodel",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Boolean>dodel(String id,
                                     HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Boolean> returnData = new ReturnData<Boolean>();
        goodsService.doDeleteGoods(getUser(request),id);
        returnData.setEntity(true);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="grllery/doupdate",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Boolean>grllery_doupdate(Goods entity ,
                                      HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Boolean> returnData = new ReturnData<Boolean>();
        String galleryId = request.getParameter("galleryId");
        String goods_imgDesc = request.getParameter("goods_imgDesc");

        goodsService.updateGoodsGallery(getUser(request), galleryId,goods_imgDesc);


        returnData.setEntity(new Boolean(true));
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="grllery/dodel",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Boolean>grllery_dodel(Goods entity ,
                                                HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Boolean> returnData = new ReturnData<Boolean>();
        String galleryId = request.getParameter("id");

        goodsService.dodelGoodsGallery(getUser(request), galleryId);


        returnData.setEntity(new Boolean(true));
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }



}
